package com.ifp.opengate.boot.config;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.ifp.adapter.AdapterHandle;
import com.ifp.core.base.SystemConf;
import com.ifp.core.log.LogHandle;
import com.ifp.gateway.bean.Channel;
import com.ifp.gateway.connector.HttpConnector;
import com.ifp.gateway.connector.IConnector;
import com.ifp.gateway.handle.GatewayHandle;
import com.ifp.gateway.service.HttpService;
import com.ifp.gateway.service.IService;
import com.ifp.opengate.boot.factory.IFPReadBodyPredicateFactory;
import com.ifp.opengate.boot.gateway.formatter.JsonFormatter;

@Configuration
public class OpenGateWayConfig {

    @Autowired
    public JsonFormatter jsonFormatter;

    // @Bean
    // public OAuthGlobalFilter getOAuthGlobalFilter()
    // {
    // return new OAuthGlobalFilter();
    // }
    @Bean
    public IFPReadBodyPredicateFactory getIFPReadBodyPredicateFactory() {
        return new IFPReadBodyPredicateFactory();
    }

    @Bean(initMethod = "init")
    public AdapterHandle adapterHandle() {
        return new AdapterHandle();
    }

    // @Bean
    // public SpringContextsUtil springContextsUtil(){
    // return new SpringContextsUtil();
    // }

    // @Bean
    // public RequestInitFilter getParamInitFilter() {
    // return new RequestInitFilter();
    // }

    @Bean
    public GatewayHandle gatewayHandle() {
        return new GatewayHandle();
    }

    @Bean("oauthConnector")
    public HttpConnector oauthConnector() {
        HttpConnector connector = new HttpConnector();
        connector.setContentType("application/json;charset=UTF-8");
        connector.setUrl("http://127.0.0.1:8067/oauth/verification");
        return connector;
    }

    @Bean("oauthServer")
    public HttpService oauthServer(@Qualifier("oauthConnector") HttpConnector connector) {
        HttpService httpService = new HttpService();
        httpService.setFormater(jsonFormatter);
        httpService.setRequestFormater(jsonFormatter);
        List<IConnector> connectorList = new ArrayList<>();
        connectorList.add(connector);
        httpService.setConnectorList(connectorList);
        return httpService;
    }

    @Bean("oauth")
    public Channel oauthChannel(@Qualifier("oauthServer") HttpService httpService) {
        Channel channel = new Channel();
        Map<String, IService> map = new HashMap<>();
        map.put("oauthServer", httpService);
        channel.setInterfaceMap(map);
        return channel;
    }

    @Bean("gatewayHandle")
    public GatewayHandle gatewayHandle(@Qualifier("oauth") Channel oauth) {
        GatewayHandle gatewayHandle = new GatewayHandle();
        Map<String, Channel> map = new HashMap<>();
        map.put("oauth", oauth);
        gatewayHandle.setChannelMap(map);
        return gatewayHandle;
    }

    @Bean
    public SystemConf systemConf() {
        return new SystemConf();
    }

    @Bean
    public LogHandle logHandle() {
        return new LogHandle();
    }

}
